package thread.learn;

/*
 * public final void join():等待该线程终止。
 */
public class ThreadJoinDemo {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread t2 = new Thread(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread t3 = new Thread();

        t1.setName("李元霸");
        t2.setName("程咬金");
        t3.setName("宇文成都");

        t1.start();
        try {
            // 子线程join了之后,主线程会阻塞等待当前子线程运行结束后,自己才接着往下执行
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("主线程耗费: " + (System.currentTimeMillis() - start));
        t2.start();
        try {
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //主线程join就卡死了
//        try {
//            Thread.currentThread().join();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        t3.start();

        System.out.println("主线程耗费: " + (System.currentTimeMillis() - start));
    }
}